package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSON;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HResourceRelationMapper;
import com.flea.hclv.model.dao.HVisitorMapper;
import com.flea.hclv.model.entity.HResourceRelation;
import com.flea.hclv.model.entity.HVisitor;
import com.flea.hclv.service.HVisitorService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.utils.FleaStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: project
 * @description: 游客
 * @author: shiliqiang
 * @create: 2018-09-26 20:50
 * @version: 1.0
 */
@Service
public class HVisitorServiceImpl implements HVisitorService {

    @Autowired
    private HVisitorMapper hVisitorMapper;
    @Autowired
    private HResourceRelationMapper hResourceRelationMapper;

    @Override
    public ServerResponse<String> saveOrUpdate(String userId, OrgDept currentUserDept, HVisitor visitor, List<HResourceRelation> hResourceRelations) throws Exception {
        if (hResourceRelations.size()==0){
            return ServerResponse.createByErrorMessage("图片为空");
        }
        if (visitor==null){
            return ServerResponse.createByErrorMessage("游客信息为空");
        }
        String id = visitor.getId();
        int result;
        //根据年龄判断 游客类型
        if (id == null) {
            id = FleaStringUtils.getGenerateUUID();
            visitor.setId(id);
            visitor.setDeptId(currentUserDept==null? HConstant.HCLV_ADMIN_ADMIN:currentUserDept.getDeptUuid());
            visitor.setCreateUser(userId);
            visitor.setModifyUser(userId);
            result = hVisitorMapper.insertSelective(visitor);
            if (result<0){
                return ServerResponse.createByErrorMessage("游客添加失败");
            }
            return insertResourceRelation(hResourceRelations,id,userId);
        }else {
            visitor.setModifyUser(userId);
            result = hVisitorMapper.updateByPrimaryKeySelective(visitor);
            if (result<0){
                return ServerResponse.createByErrorMessage("游客更新失败");
            }
            result =hResourceRelationMapper.deleteByUuid(id);
            if (result<0){
                return ServerResponse.createByErrorMessage("证件照更新失败");
            }
            return insertResourceRelation(hResourceRelations,id,userId);
        }
    }


    private ServerResponse<String> insertResourceRelation(List<HResourceRelation> hResourceRelations, String uuid, String userId) throws Exception {
        for (HResourceRelation hResourceRelation : hResourceRelations){
            String id = FleaStringUtils.getGenerateUUID();
            hResourceRelation.setId(id);
            hResourceRelation.setUuid(uuid);
            hResourceRelation.setCreateUser(userId);
            hResourceRelation.setModifyUser(userId);
            int result =hResourceRelationMapper.insertSelective(hResourceRelation);
            if (result<=0){
                throw new Exception("添加图片失败");
            }
        }
        return ServerResponse.createBySuccessMessage("操作成功");
    }

    @Override
    public ServerResponse<HVisitor> selectByVisitorId(String visitorId) {
        HVisitor visitor = hVisitorMapper.selectByPrimaryKey(visitorId);
        return ServerResponse.createBySuccess(visitor);
    }

    @Override
    public ServerResponse<List<HVisitor>> selectByIdsList(String idList) {
        if (StringUtils.isBlank(idList)){
            return ServerResponse.createByErrorMessage("游客id为空");
        }
        List<String> ids = JSON.parseArray(idList,String.class);
        List<HVisitor> list = hVisitorMapper.findByIds(ids);
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse<FleaPage<HVisitor>> selectByDept(OrgDept currentUserDept, String name1, int curPage, int pageSize) {
        RowBounds rowBounds = new RowBounds(curPage,pageSize);
        List<HVisitor> list;
        if (currentUserDept==null){
            return ServerResponse.createByErrorMessage("没有找到该用户的部门信息");
        }
        StringBuilder name = new StringBuilder();
        String deptUuid  = currentUserDept.getDeptUuid();
        if (name1.equals("")){
            // 创建时间倒排
            list = hVisitorMapper.selectByDept(deptUuid,rowBounds);
        }else {
            name.append("%");
            name.append(name1);
            name.append("%");
            //按名字模糊配
            list = hVisitorMapper.selectLikeNameAndDeptId(String.valueOf(name),deptUuid,rowBounds);
        }
        FleaPage<HVisitor> fleaPage = new FleaPage<>(list);
        return ServerResponse.createBySuccess(fleaPage);
    }

    @Override
    public ServerResponse<FleaPage<HVisitor>> selectByDeptId(OrgDept currentUserDept, int curPage, int pageSize, String zhName, String enName) {
        RowBounds rowBounds = new RowBounds(curPage,pageSize);

        if (currentUserDept==null){
            return ServerResponse.createByErrorMessage("没有找到该用户的部门信息");
        }
        StringBuilder zhName1 = new StringBuilder();
        zhName1.append("%");
        zhName1.append(zhName);
        zhName1.append("%");
        StringBuilder enName1 = new StringBuilder();
        enName1.append("%");
        enName1.append(enName);
        enName1.append("%");
        List<HVisitor> list = hVisitorMapper.selectByDeptId(rowBounds,currentUserDept.getDeptUuid(), String.valueOf(zhName1), String.valueOf(enName1));
        return ServerResponse.createBySuccess(new FleaPage<>(list));
    }

    @Override
    public ServerResponse<FleaPage<HVisitor>> selectAll(OrgDept currentUserDept, int curPage, int pageSize, String zhName, String enName){
        RowBounds rowBounds = new RowBounds(curPage,pageSize);
        List<HVisitor> list = hVisitorMapper.selectAll(rowBounds,zhName,enName);
        return ServerResponse.createBySuccess(new FleaPage<>(list));
    }

    @Override
    public ServerResponse<HVisitor> save(String userId, OrgDept currentUserDept, HVisitor visitor, HResourceRelation hResourceRelation) throws Exception {
        if (hResourceRelation==null){
            return ServerResponse.createByErrorMessage("图片为空");
        }
        if (visitor==null){
            return ServerResponse.createByErrorMessage("游客信息为空");
        }
        int result;
        String id = FleaStringUtils.getGenerateUUID();
        visitor.setId(id);
        visitor.setDeptId(currentUserDept == null ? HConstant.HCLV_ADMIN_ADMIN : currentUserDept.getDeptUuid());
        visitor.setCreateUser(userId);
        visitor.setModifyUser(userId);
        result = hVisitorMapper.insertSelective(visitor);
        List<HResourceRelation> list = new ArrayList<>();
        list.add(hResourceRelation);
        if (result > 0) {
            ServerResponse<String> stringServerResponse = insertResourceRelation(list,id,userId);
            if (stringServerResponse.isSuccess()){
                return ServerResponse.createBySuccess(visitor);
            }
        }
        return ServerResponse.createByError();
    }

    @Override
    public ServerResponse<List<HResourceRelation>> selectVisitorImage(String visitorId) {
        List<HResourceRelation> list = hResourceRelationMapper.selectByUuid(visitorId);
        if (list.size()==0){
            return ServerResponse.createByErrorMessage("图片不存在");

        }
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse<List<HVisitor>> selectByName(OrgDept currentUserDept, String visitorName) {
        if (currentUserDept==null) {
            List<HVisitor> hVisitors = hVisitorMapper.selectByName(visitorName);
            return ServerResponse.createBySuccess(hVisitors);
        }
        List<HVisitor> hVisitors = hVisitorMapper.selectByNameAndDeptId(visitorName,currentUserDept.getDeptUuid());

        return ServerResponse.createBySuccess(hVisitors);
    }

    @Override
    public ServerResponse<String> deleteByIds(String userId, List<String> idList) {
        int result = hVisitorMapper.deleteByIds(idList,userId);
        if (result<0){
            return ServerResponse.createByErrorMessage("删除失败");
        }
        return ServerResponse.createBySuccessMessage("删除成功");
    }
}
